package com.framework.admin.schedule.entity;

import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.framework.admin.schedule.constant.ScheduleConstants;
import com.framework.admin.schedule.util.CronUtils;
import com.framework.common.util.StringUtils;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;

@Data
@Entity
@Table
@org.hibernate.annotations.Table(appliesTo = "sys_job", comment = "任务执行器")
public class SysJob implements Serializable {
    private static final long serialVersionUID = 1L;

    /** 任务ID */
    @Id
    @TableId("job_id")
    private Long jobId;

    /** 任务名称 */
    @Column(columnDefinition = "varchar(50) COMMENT '任务名称'")
    private String jobName;

    /** 任务组名 */
    @Column(columnDefinition = "varchar(50) COMMENT '任务组名'")
    private String jobGroup;

    /** 调用目标字符串 */
    @Column(columnDefinition = "varchar(50) COMMENT '调用目标字符串'")
    private String invokeTarget;

    /** cron执行表达式 */
    @Column(columnDefinition = "varchar(20) COMMENT 'cron执行表达式'")
    private String cronExpression;

    /** cron计划策略 */
    @Column(columnDefinition = "varchar(1) COMMENT 'cron计划策略'")
    private String misfirePolicy = ScheduleConstants.MISFIRE_DEFAULT;

    /** 是否并发执行（0允许 1禁止） */
    @Column(columnDefinition = "varchar(1) COMMENT '是否并发执行'")
    private String concurrent;

    /** 任务状态（0正常 1暂停） */
    @Column(columnDefinition = "varchar(1) COMMENT '任务状态'")
    private String status;

    public Long getJobId()
    {
        return jobId;
    }

    public void setJobId(Long jobId)
    {
        this.jobId = jobId;
    }

    @NotBlank(message = "任务名称不能为空")
    @Size(min = 0, max = 64, message = "任务名称不能超过64个字符")
    public String getJobName()
    {
        return jobName;
    }

    public void setJobName(String jobName)
    {
        this.jobName = jobName;
    }

    public String getJobGroup()
    {
        return jobGroup;
    }

    public void setJobGroup(String jobGroup)
    {
        this.jobGroup = jobGroup;
    }

    @NotBlank(message = "调用目标字符串不能为空")
    @Size(min = 0, max = 500, message = "调用目标字符串长度不能超过500个字符")
    public String getInvokeTarget()
    {
        return invokeTarget;
    }

    public void setInvokeTarget(String invokeTarget)
    {
        this.invokeTarget = invokeTarget;
    }

    @NotBlank(message = "Cron执行表达式不能为空")
    @Size(min = 0, max = 255, message = "Cron执行表达式不能超过255个字符")
    public String getCronExpression()
    {
        return cronExpression;
    }

    public void setCronExpression(String cronExpression)
    {
        this.cronExpression = cronExpression;
    }

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getNextValidTime()
    {
        if (StringUtils.isNotEmpty(cronExpression))
        {
            return CronUtils.getNextExecution(cronExpression);
        }
        return null;
    }

    public String getMisfirePolicy()
    {
        return misfirePolicy;
    }

    public void setMisfirePolicy(String misfirePolicy)
    {
        this.misfirePolicy = misfirePolicy;
    }

    public String getConcurrent()
    {
        return concurrent;
    }

    public void setConcurrent(String concurrent)
    {
        this.concurrent = concurrent;
    }

    public String getStatus()
    {
        return status;
    }

    public void setStatus(String status)
    {
        this.status = status;
    }
}
